const rp = require('request-promise')
const APPID = 'wxfe78f0dacd759e16'
const APPSECRET = 'c4ce1f83890c598f8f941b8aebf76c59'
const URL = `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${APPID}&secret=${APPSECRET}`
const fs =  require('fs')
const path = require('path')
const fileName = path.resolve(__dirname,'./access_token.js')

const updateAccessToken = async(ctx)=>{
    const resStr = await rp(URL)
    const res = JSON.parse(resStr)
    //写文件
    if(res.access_token){
        fs.writeFileSync(fileName,JSON.stringify({
            access_token:res.access_token,
            createTime:new Date()
        }))
    }else{
        await updateAccessToken()
    }
}

const getAccessToken = async()=>{
    //读取文件
    try {
        const readRes = fs.readFileSync(fileName,'utf8')
        const readObj = JSON.parse(readRes)
        const createTime =new Date(readObj.createTime).getTime()
        const nowTime = new Date().getTime()
        if((nowTime-createTime) / 1000 / 60 / 60 >= 2){
            await updateAccessToken()
            await getAccessToken()
        }
        return readObj.access_token
    } catch (error) {
        await updateAccessToken()
        await getAccessToken()
    }
    
}
//2小时更新token
setInterval(async () => {
    await updateAccessToken()
}, (7200-300)*1000);

module.exports = getAccessToken